package com.kolon.business.mKolonInt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonInt.controller.MKolonIntSearchListController")
public class MKolonIntSearchListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mKolonInt/mKolonIntSearchList.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
		
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		int pageInt = ComUtil.sToi(page) * 15;
		
		String SearchTitle = request.getParameter("SearchTitle");
		
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		
		List mapList = new ArrayList();
		Map map = new HashMap();

		
		
			// 검색
			buffer.append("/kolon_int.aspx?strMode=3");
			buffer.append("&SearchUserName=");
			buffer.append("&SearchTitle=").append(ComUtil.getEncode(request.getParameter("SearchTitle")==null ? "ㄱㄱ" : SearchTitle,"EUC-KR"));
			buffer.append("&SearchStartDate=").append(request.getParameter("SearchStartDate")==null ? "" : request.getParameter("SearchStartDate"));
			buffer.append("&SearchEndDate=").append(request.getParameter("SearchEndDate")==null ? "" : request.getParameter("SearchEndDate"));
			buffer.append("&SortFieldName=").append("SortCreateDate");
			buffer.append("&SortAscDesc=").append("desc");
			buffer.append("&Page=").append("1");
			buffer.append("&ViewRowCount=").append(pageInt);
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("BoardList");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("ArticleCD", el.getChild("ArticleCD").getText());
				map.put("RowNum", el.getChild("RowNum").getText());
				map.put("UserID", el.getChild("UserID").getText());
				map.put("UserName", el.getChild("UserName").getText());
				map.put("BoardCD", el.getChild("BoardCD").getText());
				//map.put("Title", ComUtil.subString(el.getChild("Title").getText(),15));
				map.put("Title", el.getChild("Title").getText());
				//map.put("SelReply", el.getChild("SelReply").getText());
				map.put("UCreateDate", el.getChild("UCreateDate").getText().substring(0,10));
				map.put("ExpireDate", el.getChild("ExpireDate").getText());
				map.put("ViewCnt", ComUtil.addComma(el.getChild("ViewCnt").getText()));
				map.put("FileOnly", el.getChild("FileOnly").getText());
				map.put("Importance", el.getChild("Importance").getText());
				map.put("AttachCNT", el.getChild("AttachCNT").getText());
				map.put("CommentCNT", el.getChild("CommentCNT").getText());
				map.put("Ref_Seq", el.getChild("Ref_Seq").getText());
				map.put("Ref_Level", el.getChild("Ref_Level").getText());
				map.put("SubCnt", el.getChild("SubCnt").getText());
				map.put("IsNotice", el.getChild("IsNotice").getText());
				map.put("B_ACL", el.getChild("B_ACL").getText());
				map.put("Gubun", el.getChild("Gubun").getText());
				map.put("Company", el.getChild("Company").getText());
				map.put("Price", el.getChild("Price").getText());
				map.put("Area", el.getChild("Area").getText());
				map.put("Status", el.getChild("Status").getText());
				map.put("Phone", el.getChild("Phone").getText());
				map.put("Address", el.getChild("Address").getText());
				map.put("Theme", el.getChild("Theme").getText());
				map.put("Point", el.getChild("Point").getText());
				//map.put("UCStatus", el.getChild("UCStatus").getText());
				//map.put("TelOffice", el.getChild("TelOffice").getText());
				//map.put("Mobile", el.getChild("Mobile").getText());
				mapList.add(map);
			}			
		
		
		// TableInfo 영역
		Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("TableInfo");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		Map map2 = new HashMap();
		// XML 데이터 
		if (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map2 = new HashMap();
			map2.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			map2.put("TotalNoticeRecortCnt", el.getChild("TotalNoticeRecortCnt").getText());
			map2.put("TotalPage", el.getChild("TotalPage").getText());
			map2.put("CurrPage", el.getChild("CurrPage").getText());
			map2.put("OnePageCnt", el.getChild("OnePageCnt").getText());
			map2.put("bPublicWrite", el.getChild("bPublicWrite").getText());
			map2.put("UserID", el.getChild("UserID").getText());
			map2.put("SAdminCNT", el.getChild("SAdminCNT").getText());
			//mapList2.add(map);
		}

		
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("mapList2", map2);
		
		modelAndView.addObject("strMode", request.getParameter("strMode")==null ? "18" : request.getParameter("strMode"));
		//modelAndView.addObject("BoardCD", request.getParameter("BoardCD"));
		modelAndView.addObject("Page", ComUtil.sToi(page)+1);
		modelAndView.addObject("ViewRowCount", (ComUtil.sToi(page)+1) * 15);
		//modelAndView.addObject("B_IsSub", request.getParameter("B_IsSub"));
		modelAndView.addObject("Depth", request.getParameter("Depth"));
		modelAndView.addObject("SearchTitle", SearchTitle);
		modelAndView.addObject("SearchStartDate", request.getParameter("SearchStartDate"));
		modelAndView.addObject("SearchEndDate", request.getParameter("SearchEndDate"));
		
		return modelAndView;
	}

}